﻿@if(ViewBag.PageCount > 1)
{
    if(ViewBag.PageIndex < 0)
    {
        ViewBag.PageIndex = ViewBag.PageCount - 1;
    }
    var loc = ViewBag.PageRoute as Loc;
        
    <nav class="pager">
        <div>
        @if(ViewBag.PageIndex > 0)
        {
            <a href="@Url.Action(loc.Copy().Set(ViewBag.PageParam, 0) as Loc)">@T(Res.In("Common").Of("Vocab").Name("First"))</a>
            <a href="@Url.Action(loc.Copy().Set(ViewBag.PageParam, ViewBag.PageIndex - 1) as Loc)">@T(Res.In("Common").Of("Vocab").Name("Prev"))</a>
        }
        else
        {
            <a class="inactive" href="#">@T(Res.In("Common").Of("Vocab").Name("First"))</a>
            <a class="inactive" href="#">@T(Res.In("Common").Of("Vocab").Name("Prev"))</a>
        }
        </div>
        <div>
        @{
            dynamic s, e;

            if(ViewBag.PageNum >= ViewBag.PageCount)
            {
                s = 0;
                e = ViewBag.PageCount;
            }
            else if(ViewBag.PageIndex - ViewBag.PageNum / 2 < 0)
            {
                s = 0;
                e = ViewBag.PageNum < ViewBag.PageCount ? ViewBag.PageNum : ViewBag.PageCount;
            }
            else if(ViewBag.PageIndex + ViewBag.PageNum / 2 < ViewBag.PageCount)
            {
                s = ViewBag.PageIndex - ViewBag.PageNum / 2;
                e = ViewBag.PageIndex + ViewBag.PageNum / 2;
            }
            else
            {
                s = ViewBag.PageCount - ViewBag.PageNum;
                e = ViewBag.PageCount;
            }
            for(var i = s; i < e; i++)
            {
                if(i == ViewBag.PageIndex)
                {
                    <a class="current" href="#" title="@(i + 1)">@(i + 1)</a>
                }
                else
                {
                    <a href="@Url.Action(loc.Copy().Set(ViewBag.PageParam, i) as Loc)" title="@(i + 1)">@(i + 1)</a>
                }
            }
        }
        </div>
        <div>
        @if(ViewBag.PageIndex < ViewBag.PageCount - 1)
        {
            <a href="@Url.Action(loc.Copy().Set(ViewBag.PageParam, ViewBag.PageIndex + 1) as Loc)">@T(Res.In("Common").Of("Vocab").Name("Next"))</a>
            <a href="@Url.Action(loc.Copy().Set(ViewBag.PageParam, ViewBag.PageCount - 1) as Loc)">@T(Res.In("Common").Of("Vocab").Name("Last"))</a>
        }
        else
        {
            <a class="inactive" href="#">@T(Res.In("Common").Of("Vocab").Name("Next"))</a>
            <a class="inactive" href="#">@T(Res.In("Common").Of("Vocab").Name("Last"))</a>
        }
        </div>
    </nav>
}